package com.train.extendsThread;
/**
 * 继承{@link Thread}类，并重写{@link Thread#run()}方法启动线程
 *
 * 用类实例调用{@link Thread#start()}方法
 * */
public class ExtendsThread extends Thread{
    public ExtendsThread(){}

    /**
     * 构造方法：设置当前实例的线程名和优先级
     * @param threadName 自定义线程名
     * @param priority 线程优先级（1-10，默认5）
     * */
    public ExtendsThread(String threadName,Integer priority){
        if ( priority > MAX_PRIORITY || priority < MIN_PRIORITY) {
            throw new IllegalArgumentException("参数错误");
        }
        super.setPriority(priority);// 设置线程优先级
        super.setName(threadName);
    }
    /**
     * 循环10次输出 当前线程名+is running 的字符串，每次输出前让当前线程睡100ms
     * */
    @Override
    public void run() {
//        realize_1();
        realize_2();
    }

    public void realize_1(){
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(100);
                System.out.println(this.getName()+" is running");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void realize_2(){
        int i = 0;
        while (true){
            if ( ++i > 100) {
                Thread.currentThread().stop();//这种方式不推荐，建议使用标志位的形式实现
            }
            System.out.println(this.getName()+" is running. i:"+i);
        }
    }
}
